package com.cjl.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cjl.auth.mapper.SysRoleMapper;
import com.cjl.auth.service.SysRoleService;
import com.cjl.auth.service.SysUserRoleService;
import com.cjl.auth.service.SysUserService;
import com.cjl.model.system.SysRole;
import com.cjl.model.system.SysUser;
import com.cjl.model.system.SysUserRole;
import com.cjl.vo.system.AssignRoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysUserRoleService sysUserRoleService;

    @Autowired
    private SysUserService sysUserService;

    @Override
    public Map<String, Object> findRoleDataByUserId(Long userId) {
        //所有角色
        List<SysRole> allRoleList = baseMapper.selectList(null);

        LambdaQueryWrapper<SysUserRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUserRole::getUserId, userId);

        List<SysUserRole> userAssignRoleList = sysUserRoleService.list(wrapper);
        List<Long> userAssignRoleIdList = userAssignRoleList.stream().map(item -> item.getRoleId()).collect(Collectors.toList());
        //用户当前拥有的角色
        List<SysRole> assignRoleList = new ArrayList<>();
        for (SysRole sysRole : allRoleList) {
            //比较
            if (userAssignRoleIdList.contains(sysRole.getId())) {
                assignRoleList.add(sysRole);
            }
        }

        Map<String, Object> roleMap = new HashMap<>();
        roleMap.put("assignRoleList", assignRoleList);
        roleMap.put("allRolesList", allRoleList);
        return roleMap;
    }

    @Override
    public void doAssign(AssignRoleVo assignRoleVo) {
        LambdaQueryWrapper<SysUserRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUserRole::getUserId, assignRoleVo.getUserId());
        sysUserRoleService.remove(wrapper);

        List<Long> roleIdList = assignRoleVo.getRoleIdList();
        List<SysUserRole> sysUserRoleList = new ArrayList<>();
        for (Long roleId : roleIdList) {
            if (StringUtils.isEmpty(roleId)) {
                continue;
            }
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setUserId(assignRoleVo.getUserId());
            sysUserRole.setRoleId(roleId);
            sysUserRoleList.add(sysUserRole);
        }
        sysUserRoleService.saveBatch(sysUserRoleList);


        SysUser sysUser = sysUserService.getById(assignRoleVo.getUserId());
        sysUser.setDeptId(assignRoleVo.getDeptId());
        sysUser.setPostId(assignRoleVo.getPostId());
        sysUserService.updateById(sysUser);
    }
}
